
from emutils import time_utils
from datetime import *
import leancloud
import emmongo.em_mongomanager as mongoManager
from LSTM.variableset import EMVar

PENDING = 1
DONE = 0
LEAN_MODEL_NAME = 'SelfFundSetModel'
TRADE_SELL = 0
TRADE_BUY = 1
PREDICT_GLOABL_DEVITATION = 'GlobalOffset'
PREDICT_LAST_DEVITATION = 'LastDayOffset'
PREDICT_MIN_VALUE = 'PredictMIN'
PREDICT_MAX_VALUE = 'PredictMAX'
PREDICT_DEFAULT = '无'

class SelfFundSetModel(object):


    def __init__(self ,stkNo, stkName, price, predictBaseAccToClose, predict_base = PREDICT_DEFAULT,tradeType=TRADE_SELL, precent=0, totalMoney=15000, quickBuy=False ,pending=PENDING
                 ,startDay=time_utils.get_next_offset_work_day(), endDay=time_utils.get_next_offset_work_day(offset=4)
                 ,startTime='09:40', endTime='15:00', createDate=datetime.today(), strategy_name=''):
        self.stkNo = stkNo
        self.stkName = stkName
        self.price = price
        self.tradeType = tradeType
        self.precent = precent
        self.totalMoney = totalMoney
        self.quickBuy = quickBuy
        self.pending = pending
        self.startDay = startDay
        self.endDay = endDay
        self.startTime = startTime
        self.endTime = endTime
        self.createDate = createDate
        self.predict_base = startDay.strftime('%m-%d') + predict_base
        self.predictBaseAccToClose = predictBaseAccToClose
        self.strategy_name = strategy_name


    def to_dict(self):
        return dict((name, getattr(self, name)) for name in dir(self) if not name.startswith('__') and not callable(getattr(self, name)))

    def save_to_lean(self):
        dic = self.to_dict()
        LeanModel = leancloud.Object.extend(LEAN_MODEL_NAME)
        lean_model = LeanModel()
        for key, value in dic.items():
            lean_model.set(key, value)
        lean_model.save()

    def save_to_mongo(self, tag=''):
        v = self.__dict__.copy()
        v['tag'] = tag
        # v['createDate'] = EMVar.STR_NOW_YMD
        mongoManager.db[mongoManager.COL_SELF_FUND_SET].update_one({'stkNo': self.stkNo,
                                                                    'createDate': self.createDate,
                                                                    'strategy_name': self.strategy_name},
                                                                   {'$set': v}, upsert=True)


# sellModel = SelfFundSetModel(stkName='600345', stkNo='600345',
#                              price=0.26, tradeType=TRADE_SELL)
# sellModel.save_to_lean()


